/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.parser.mp.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;

/**
 *
 * @author dell
 */
public class ParserJson {

    private static final Logger logger = Logger.getLogger(ParserJson.class.getName());

    public static Map<String, String> getContent(String jsonString) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
//        JSONObject object = JSONObject.fromObject(jsonString);
//        for (Iterator<?> iter = object.keys(); iter.hasNext();) {
//            String key = (String) iter.next();
//            String value = object.get(key).toString();
//            map.put(key, value);
//        }
        JSONObject object = JSONObject.parseObject(jsonString);
        for (Map.Entry<String, Object> entrySet : object.entrySet()) {
            String key = entrySet.getKey();
            Object value = entrySet.getValue();
            map.put(key, (String) value);
        }
        return map;
    }

    public static Map[] ConversionArrayMap(String jsonString) throws Exception {
        JSONArray jsonArray = JSONArray.parseArray(jsonString);
        Map[] dataMap = new HashMap[jsonArray.size()];
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            HashMap<String, String> map = new HashMap<String, String>();
//            for (Iterator<?> iter = jsonObject.keys(); iter.hasNext();) {
//                String key = (String) iter.next();
//                String value = jsonObject.get(key).toString();
//                map.put(key, value);
//            }
            for (Map.Entry<String, Object> entrySet : jsonObject.entrySet()) {
                String key = entrySet.getKey();
                Object value = entrySet.getValue();
                map.put(key, (String) value);
            }
            dataMap[i] = map;
        }
        return dataMap;
    }

    public static String getType(String jsonString) {
        JSONObject jsonObject = JSONObject.parseObject(jsonString);
        return jsonObject.get("data_type").toString();
    }

    public static long getAcceptTime(String jsonString) {
        JSONObject jsonObject = JSONObject.parseObject(jsonString);
        long ms = Long.parseLong(jsonObject.get("accept_time").toString());
        return ms;
    }

    public static List<Map<String, String>> getContentByRecord_list(String jsonString) throws Exception {
        JSONObject object = JSONObject.parseObject(jsonString);

        JSONArray dataArray = object.getJSONArray("record_list");
        List<Map<String, String>> mapArray = new ArrayList<Map<String, String>>();
        for (int i = 0; i < dataArray.size(); i++) {
            JSONObject jsonObject = dataArray.getJSONObject(i);
            Map<String, String> map = new HashMap<String, String>();
//            for (Iterator<?> iter = jsonObject.keys(); iter.hasNext();) {
//                String key = (String) iter.next();
//                String value = jsonObject.get(key).toString();
//                map.put(key, value);
//                logger.debug(key + "= " + value);
//            }
            for (Map.Entry<String, Object> entrySet : jsonObject.entrySet()) {
                String key = entrySet.getKey();
                Object value = entrySet.getValue();
                if (key.equals("content")) {
//                    解析content
                    JSONObject content = JSONObject.parseObject(value.toString());
                    for (Map.Entry<String, Object> entrySet1 : content.entrySet()) {
                        String key1 = entrySet1.getKey();
                        Object value1 = entrySet1.getValue();
                        map.put(key1, value1.toString());
                    }

                }
                map.put(key, value.toString());
//                logger.info(key + "= " + value);
            }
            mapArray.add(map);
        }
        return mapArray;
    }

}
